//app.js
App({

  // 全局参数
  globalData: {
    config: require('./config.js').Config,

    userInfo: '',
    pluginList: [],
    wxappStyle: {},
    wxUserInfo: null
  },

  // 工具类
  utils: require('./utils/util.js'),
  // 服务类
  server: require('./utils/service.js'),

  onLaunch: function() {
    console.log(wx.getSystemInfoSync());
    // 初始化配置
    this.initExtConfig();

    // 校验更新
    this.utils.updateWxApp();
    // 校验授权
    this.utils.isAuthorize(this);

    this.globalData.systemInfo = wx.getSystemInfoSync();
    this.globalData.windowHeight = wx.getSystemInfoSync().windowHeight;
    this.globalData.windowWidth = wx.getSystemInfoSync().windowWidth;
    this.globalData.userInfo = wx.getStorageSync('userInfo') || {};
    this.globalData.openid = wx.getStorageSync('openid') || '';
  },

  // 自定义导航栏
  // onShow(e) {
  //   console.log(e);
  //   // 将小程序的进入页面设置为小程序默认首页
  //   !this.noRefleshAgentPageUrl && (this.globalData.config['SwitchTabToAgentPageUrl'] = e.path);
  //   console.log(this.globalData)
  // },

  // onHide() {
  //   this.noRefleshAgentPageUrl = true;
  // },

  // 获取第三方参数,初始化配置
  initExtConfig() {
    // 第三方参数
    const extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    console.log('=========getExtConfigSync获取第三方参数=========')
    console.log(extConfig);
    if (extConfig.domain) {
      this.globalData.config['url'] = extConfig['domain'] + '/crm-api/';
    }
    console.log('=========最终参数=========')
    console.log(extConfig);
    console.log(this.globalData);
  },

  // 页面初始化赋值
  pageOnloadInit(that, app) {
    if (app.globalData.wxappStyle['wxappletName']) {
      app.initPage(app, that);
    } else {
      app.utils.initAgentWxappStyle(app, that);
    }
    app.userInfoReadyCallback = function() {
      if (app.globalData.wxappStyle['wxappletName']) {
        app.initPage(app, that);
      } else {
        app.utils.initAgentWxappStyle(app, that);
      }
    }
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },

  // 执行初始化
  initPage(app, that) {
    const config = require('./config.js').Config;
    const mainColor = (app.globalData.wxappStyle && app.globalData.wxappStyle.wxappletColor) || '#fff';
    const wsTabbarList = app.globalData.config['selectedTarbarType'] === 'Admin' ? app.globalData.config['AdminTarbarList'] : app.globalData.config['AgentTarbarList'];
    // 设置页面默认参数
    that && that.setData({
      // api地址
      "url": config.url,
      // 图片域名
      // "imageUrl": config['ENV'] === 'LOCAL' ? config.url : '',
      "imageUrl": '',
      // 首頁九宫格样式
      menuType: app.globalData.wxappStyle.menuType || 1,
      // 小程序主色
      "wxappletColor": mainColor,

      'tabbarConfig': {
        // tabbar列表
        "wsTabbarList": wsTabbarList,
        // 是否显示tabbar
        'isShowTabbar': app.utils.contrastTabbarList(that.route, wsTabbarList),
        // 选中的是第几项
        'tabbarCurrent': app.utils.getArrayIndex(that.route, wsTabbarList)
      }
    });

    // 设置顶部导航栏样式
    wx.setNavigationBarColor({
      frontColor: '#ffffff', // 字体颜色
      backgroundColor: mainColor, // 背景色
    });

    // 设置上拉下拉背景色
    wx.setBackgroundColor({
      backgroundColorTop: mainColor, // 顶部窗口的背景色为白色
      backgroundColorBottom: '#ffffff', // 底部窗口的背景色为白色
      complete: function(res) {
        console.log(res)
      }
    });

    that && (that.getFormId = function(e) {
      console.log('====addWxappFormId====');
      console.log(e.detail.formId);

      app.globalData.userInfo && app.server.getConnect({
        point_url: app.server.agentApi.addFormId,
        formId: e.detail.formId,
        id: app.globalData.userInfo.id || wx.getStorageSync('userInfo').id
      });
    })

    that && (that.onShareAppMessage = function() {
      console.log('======转发路径=======');
      console.log(app.utils.getCurrentPageUrlWithArgs());
      return {
        title: that.data.shareAppMessageTitle || app.globalData['wxappStyle']['wxappletName'] || wx.getStorageSync('wxappStyle')['wxappletName'],
        path: app.utils.getCurrentPageUrlWithArgs() || 'pages/agent/homepage/index/index'
      }
    })
  },

  // 跳转页面
  jumpToPage: function(url, name) {
    // 当url为导航栏上的页面是需要进行判断
    wx.navigateTo({
      url: url
    })
    name && wx.setNavigationBarTitle({
      title: name,
    })
  },

  // 弹窗
  showConfirm(title, content, cancel, cb, cb2, cancelText = '取消', confirmText = '确定') {
    wx.showModal({
      title: title,
      showCancel: cancel,
      cancelText: cancelText,
      content: content,
      confirmColor: this.globalData.wxappStyle ? this.globalData.wxappStyle.wxappletColor : '#18bc9c',
      confirmText: confirmText,
      success: function(res) {
        if (res.confirm) {
          typeof cb == "function" && cb();
        } else {
          typeof cb2 == "function" && cb2();
        }
      }
    })
  }
})